/**
 * 
 */
package au.com.carboncycle.gb;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.VolatileImage;

import javax.swing.JComponent;

import au.com.carboncycle.gb.engine.GBEngine;
import au.com.carboncycle.gb.engine.graphics.GBRenderer;

/**
 * @author Brendan
 *
 */
public class GeneralBrawlerComponent extends JComponent implements FocusListener, MouseListener, KeyListener {

	private static final long serialVersionUID = 1L;

	private GBEngine engine;
	
	private AWTKeyToInputMapper keyMapper;
	
	public GeneralBrawlerComponent(int width, int height) {
		this.keyMapper = new AWTKeyToInputMapper();
		
		setPreferredSize(new Dimension(width, height));
    	addFocusListener(this);
    	addMouseListener(this);
    	addKeyListener(this);
	}

	public void start() {
        VolatileImage image = createVolatileImage(getPreferredSize().width, getPreferredSize().height);
        Graphics2D g = (Graphics2D)getGraphics();
        Graphics2D og = (Graphics2D)image.getGraphics();
				
		engine = new GBEngine(
						new GBRenderer(getGraphicsConfiguration(), 
											getBounds(), 
											image,
											g)
					);
		
		Thread engineThread = new Thread(engine);
		engineThread.start();
		
		requestFocus();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		engine.appendInput(keyMapper.mapToInputKey(e.getKeyCode()), true);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		engine.appendInput(keyMapper.mapToInputKey(e.getKeyCode()), false);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		
	}

}
